package com.guoyansoft.controller;

import com.guoyansoft.config.GetTimeDifference;
import com.guoyansoft.config.R;
import com.guoyansoft.model.pojo.HistoricalOrders;
import com.guoyansoft.model.service.HistoricalOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘耀诚
 * 历史订单业务控制器
 */
@RestController
@RequestMapping("/historicalOrders")
public class HistoricalOrdersController {
    @Autowired
    private HistoricalOrdersService historicalOrdersService;

    /**
     * 新增历史订单------待处理订单完工
     * @param historicalOrders 待处理订单实体
     * @return 响应结果
     */
    @RequestMapping("/goToHistory")
    public R goToHistory(HistoricalOrders historicalOrders,String tip,String customName){
        if(tip!=null && tip.equals("撤销订单")){
            historicalOrders.setState("已撤销");
            return historicalOrdersService.saveRevoke( historicalOrders);
        }
        Date date=new Date();
        double hours = GetTimeDifference.getHourDifference(historicalOrders.getOrderTime(), date);
        if(hours>2.0){
            historicalOrders.setState("超时完工");
        }else if(hours<=2.0) {
            historicalOrders.setState("已完工");
        }
        historicalOrders.setCompletionTime(date);
        Map<String,Object> params=new HashMap<>();
        params.put("historicalOrders",historicalOrders);
        params.put("customName",customName);
        return historicalOrdersService.save(params);
    }

    /**
     * 修改待处理订单------用户修改
     * @param historicalOrders 待处理订单实体
     * @return 响应结果
     */
    @RequestMapping("/modify")
    public R modify(HistoricalOrders historicalOrders){
        return historicalOrdersService.modify(historicalOrders);
    }

    /**
     * 修改待处理订单------用户删除
     * @param id 待处理订单实体
     * @return 响应结果
     */
    @RequestMapping("/drop")
    public R drop(Integer[] id){
        return historicalOrdersService.drop(id);
    }

    /**
     * 多条件分页展示
     * @param customPhone 客户电话
     * @param state 订单状态
     * @param curPage 当前页
     * @param pageSize 每页记录数
     * @return 响应数据
     */
    @RequestMapping("/queryByPage")
    public R queryByPage(String customPhone, String state,Integer curPage,Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("customPhone",customPhone);
        map.put("state",state);
        map.put("startIndex",(curPage-1)*pageSize);
        map.put("pageSize",pageSize);
        return historicalOrdersService.queryByPage(map);
    }

    @RequestMapping("/queryByShopName")
    public R queryByShopName(String shopName,Integer month,Integer year,Integer curPage,Integer pageSize){
        Map<String,Object> params=new HashMap<>();
        params.put("shopName",shopName);
        params.put("month",month);
        params.put("year",year);
        params.put("startIndex",(curPage-1)*pageSize);
        params.put("pageSize",pageSize);
        return historicalOrdersService.queryByShopName(params);
    }

    @RequestMapping("/queryByShopNameYear")
    public R queryByShopNameYear(String shopName,Integer year,Integer curPage,Integer pageSize){
        Map<String,Object> params=new HashMap<>();
        params.put("shopName",shopName);
        params.put("year",year);
        params.put("startIndex",(curPage-1)*pageSize);
        params.put("pageSize",pageSize);
        return historicalOrdersService.queryByShopNameYear(params);
    }
}
